package producer;

import user.list.SynchronizedBuffer;;

public class StringProducer extends Thread {

	private final int id;
	private SynchronizedBuffer<String> buffer;
	private final int timeBetweenProductions;

	public StringProducer(int id, SynchronizedBuffer<String> buffer, int timeBetweenProductions) {
		this.id = id;
		this.buffer = buffer;
		this.timeBetweenProductions = timeBetweenProductions;
	}

	private boolean doSleep() {
		try {
			Thread.sleep(this.timeBetweenProductions);
		} catch (InterruptedException e) {
			return false;
		}
		return true;
	}

	@Override
	public void run() {
		int counter = 0;
		while (doSleep()) {
			String s = String.format("%3d", id) + "p" + String.format("%5d", ++counter);
			System.out.println("PRODUCER nr." + String.format("%3d", id) + " produces: \t" + s);
			try {
				buffer.interruptablePush(s);
			} catch (InterruptedException e) {
				return;
			}
		}
	}

}
